<?php
/**
*
* @package ppkBB3cker
* @version $Id: importtorrents.php 1.000 20089-02-05 20:59:00 PPK $
* @copyright (c) 2008, 2009 PPK
* http://www.ppkbb3cker.ru, http://protoneutron.narod.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

require_once($phpbb_root_path."tracker/include/bencoding.$phpEx");

function torrents_import($data, $id)
{
	global $config, $phpbb_root_path, $db, $user, $phpEx, $forum_id, $auth;

	@set_time_limit(0);

	$tmpname = $phpbb_root_path.$config['upload_path'].'/'.$data['physical_filename'];

	if(!file_exists($tmpname))
	{
		return "[1] Torrent Error";
	}

	if(!$tmpsize=@filesize($tmpname))
	{
		return "[2] Torrent Error";
	}
	$dict=bdecode_f($tmpname, $tmpsize);

	if (!$dict)
	{
		return "[3] Torrent Error";
	}
	if(strlen(@$dict['info']['pieces']) % 20!=0)
	{
		return "[4] Torrent Error";
	}
	if(!@$dict['info']['name'] || !@$dict['info']['piece length'] || !@$dict['info']['pieces'])
	{
		return "[5] Torrent Error";
	}
	if(is_array(@$dict['info']['files']) && sizeof($dict['info']['files']) > 1)
	{
		$type = "multi";
		$filelist=@$dict['info']['files'];
	}
	else
	{
		$type = "single";
		if(isset($dict['info']['length']))
		{
			$filelist[0]['length']=$dict['info']['length'];
			$filelist[0]['path'][0]=$dict['info']['name'];
		}
		else
		{
			$filelist=@$dict['info']['files'];
		}
	}

	$forum_url=$config['server_protocol'].$config['server_name'];
	$config['script_path']=str_replace('\\', '/', $config['script_path']);
	$forum_path=$config['script_path']!='/' ? $config['script_path'] : '';
	if(!$config['ppkbb_announce_url'])
	{
		$config['ppkbb_announce_url']=$forum_path.'/tracker/announce.'.$phpEx;
	}

	$dict['announce']=$forum_url.$config['ppkbb_announce_url']."?passkey=";

	unset($dict['announce-list']); // remove multi-tracker capability
	unset($dict['azureus_properties']); // remove azureus properties
	unset($dict['nodes']); // remove cached peers (Bitcomet & Azareus)

	$private=@$dict['info']['private'] ? 1 : 0;
	//$dict['created by']="{$user->data['username']}")); // change created by
	$dict['publisher']="{$config['server_name']}"; // change publisher
	$dict['publisher.utf-8']="{$config['server_name']}"; // change publisher

	if(($config['ppkbb_tprivate_flag']==1 && $private!=1) || ($config['ppkbb_tprivate_flag']==2 && $private==1))
	{
		unset($dict['info']['crc32']); // remove crc32
		unset($dict['info']['ed2k']); // remove ed2k
		unset($dict['info']['md5sum']); // remove md5sum
		unset($dict['info']['sha1']); // remove sha1
		unset($dict['info']['tiger']); // remove tiger
	}

	if($config['ppkbb_tprivate_flag']==1)
	{
		$dict['info']['private']=1;  // add private tracker flag
		$private=1;
	}
	else if($config['ppkbb_tprivate_flag']==2)
	{
		$dict['info']['private']=0;  // remove private tracker flag
		$private=0;
	}
	else if($config['ppkbb_tprivate_flag']==-1 && $private!=1)
	{
		return "[13] Torrent Error";
	}
	else if($config['ppkbb_tprivate_flag']==-2 && $private==1)
	{
		return "[11] Torrent Error";
	}

	$enc_f=bencode($dict);

	$infohash=sha1(bencode($dict['info']));

	$res=$db->sql_query("SELECT id, post_msg_id, topic_id FROM ".TRACKER_TORRENTS_TABLE." WHERE info_hash='". $db->sql_escape($infohash) ."' LIMIT 1");
	$data=$db->sql_fetchrow($res);

	if($data['id'])
	{
		$q="SELECT p.post_approved, t.topic_id FROM ".POSTS_TABLE." p, ".TOPICS_TABLE." t WHERE t.topic_id='{$data['topic_id']}' AND p.topic_id=t.topic_id AND p.post_id='{$data['post_msg_id']}' LIMIT 1";
		$res=$db->sql_query($q);
		$data2=$db->sql_fetchrow($res);
		if($data2['topic_id'])
		{
			return "[10] Torrent Error: <a href='".$config['server_protocol'].$config['server_name'].$config['script_path'].'/viewtopic.php?t='.$data2['topic_id']."'>Duplicated Torrent</a>, Approved: <b>".($data2['post_approved'] ? 'Yes' : 'No').'</b>';
		}
		else
		{
			$r=$db->sql_query("DELETE FROM ".TRACKER_TORRENTS_TABLE." WHERE id='{$data['id']}'");
			$r=$db->sql_query("DELETE FROM ".TRACKER_FILES_TABLE." WHERE id='{$data['id']}'");
			$r=$db->sql_query("DELETE FROM ".TRACKER_THANKS_TABLE." WHERE torrent_id='{$data['id']}'");
			$r=$db->sql_query("DELETE FROM ".TRACKER_SNATCHED_TABLE." WHERE torrent='{$data['id']}'");
			$r=$db->sql_query("DELETE FROM ".TRACKER_PEERS_TABLE." WHERE torrent='{$data['id']}'");
		}
	}

	$tsize=0;

	foreach($filelist as $i => $v)
	{
		$v['length']=my_int_val($v['length']);
		$tsize+=$v['length'];
		$db->sql_query("INSERT INTO ".TRACKER_FILES_TABLE." (id, filename, size) VALUES ('$id', '". $db->sql_escape(utf8_normalize_nfc(implode('/', @$v['path']))) ."', '{$v['length']}')");
	}

	$forb = ($auth->acl_get('u_canskiptcheck') && $auth->acl_get('f_canskiptcheck', $forum_id)) ? 0 : 5;
	$db->sql_query("INSERT INTO ".TRACKER_TORRENTS_TABLE." (id, info_hash, numfiles, type, size, last_cleanup, added, forb, private) VALUES ('$id', '". $db->sql_escape($infohash) ."', '".sizeof($filelist)."', '$type', '$tsize', '".time()."', '".time()."', '$forb', '$private')");

	$fp = fopen($tmpname, "w");
	if($fp)
	{
		@fwrite($fp, $enc_f, strlen($enc_f));
		fclose($fp);
	}
	else
	{
		return "[12] Torrent Error";
	}

	return true;
}
?>
